﻿using System;
using System.Web;
using System.Web.Mvc;

namespace Web.Filter
{
    public class SysUserPermissionsFilter : BaseFilter, IActionFilter
    {

        private const string SysLoginUrl = "~/BackStage/Login";

        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
        }

        /// <summary>
        ///     Action访问前调用，检查Userid是否为空，为空未登录
        /// </summary>
        /// <param name="filterContext"></param>
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower() == "backstage" &&
                filterContext.ActionDescriptor.ActionName.ToLower() == "login")
                return;

            try
            {

                if (filterContext.ActionDescriptor.ControllerDescriptor.ControllerName.ToLower() == "backstage")
                {
                    if (HttpContext.Current.Session["SYSUSER"] == null)
                    {
                        filterContext.Result = new RedirectResult(SysLoginUrl);
                    }

                }


            }
            catch (Exception e)
            {

            }
        }
    }
}